package org.xx.armory.swing.builders;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Paths;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class ImageBoxModelBuilder
        extends ComponentModelBuilder<ImageBoxModel> {
    private final Logger logger = LoggerFactory.getLogger(ImageBoxModelBuilder.class);

    private String src;

    private String imageName;

    public ImageBoxModelBuilder setSrc(
            String src
    ) {
        this.src = src;
        return this;
    }

    public ImageBoxModelBuilder setImageName(
            String imageName
    ) {
        this.imageName = imageName;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ImageBoxModel build() {
        final String src;
        if (!isBlank(this.src)) {
            if (getLineInfo() != null) {
                src = Paths.get(getLineInfo().getPath()).resolveSibling(this.src).toString();
                logger.trace("resolve src \"{}\" to \"{}\"", this.src, src);
            } else {
                src = trimToEmpty(this.src);
            }
        } else {
            src = null;
        }

        return new ImageBoxModel(getLineInfo(), getName(), isVisible(), src, this.imageName);
    }
}
